package edu.nf.supermarket.web.controller.app;

import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.entity.Address;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yjj
 * @date 2025-05-29
 */
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;

    @PostMapping("/addAddress")
    public ResultVO<String> addAddress(@RequestBody Address address) {
        if (address.getIsDefault()) {
            addressService.defaultAddress(address.getUserId());
        }
        addressService.addAddress(address, address.getUserId());
        return success();
    }

    @PostMapping("/findAllAddress")
    public ResultVO<List<Address>> findAllAddress(@RequestParam("userId") Integer userId) {
        return success(addressService.findAllAddress(userId));
    }

    @PostMapping("/updateAddress")
    public ResultVO<String> updateAddress(@RequestBody Address address) {
        if (address.getIsDefault()) {
            addressService.defaultAddress(address.getUserId());
        }
        addressService.updateAddress(address);
        return success();
    }

    @DeleteMapping("/deleteAddress/{addressId}")
    public ResultVO<String> deleteAddress(@PathVariable("addressId") Integer addressId) {
        addressService.deleteAddress(addressId);
        return success();
    }
    @PostMapping("/setDefault")
    public ResultVO<String> setDefault(@RequestParam("userId") Integer userId,@RequestParam("addressId") Integer addressId){
        addressService.defaultAddress(userId);
        addressService.setDefault(userId,addressId);
        return success();
    }
}
